<template>
  <div class="app-container" v-loading="loading">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
    >

        <el-form-item label="会员名称" prop="inviterId">
          <el-select
              v-model="queryParams.memId"
              filterable
              clearable
              remote
              reserve-keyword
              placeholder="请输入手机号"
              style="width: 200px;"
              :remote-method="remoteMethod"
              :loading="memloading">
              <el-option
                v-for="item in memberOptions"
                :key="item.memId"
                :label="item.realname+'('+item.mobile+')'"
                :value="item.memId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="真实姓名" prop="payRealName" >
            <el-input
              v-model="queryParams.payRealName"
              placeholder="请输入真实姓名"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="申请状态" prop="status" >
           <el-select v-model="queryParams.status"  placeholder="请选择申请状态" clearable  style="width: 200px">
              <el-option
                 v-for="dict in stateObeject"
                 :key="dict.value"
                 :label="dict.label"
                 :value="dict.value"
              />
           </el-select>
        </el-form-item>
          <el-form-item label="申请时间" style="width: 450px;">
            <el-date-picker
                v-model="dateRange"
                value-format="YYYY-MM-DD HH:mm:ss"
                type="datetimerange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
            ></el-date-picker>
          </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
            <el-button
               type="warning"
               plain
               icon="Download"
               @click="handleExport"
               v-hasPermi="['withdrawal:index:export']"
            >导出</el-button>
         </el-col>
         <el-col :span="1.5">
            <el-button
               type="success"
               plain
               icon="Pointer"
              @click="handAllDeal"
               v-hasPermi="['withdrawal:index:allDeal']"
               :disabled="multiple"
            >批量处理</el-button>
         </el-col>
         <el-col :span="1.5">
            <el-button
               type="primary"
               plain
               icon="Pointer"
              @click="handAllAgree"
               v-hasPermi="['withdrawal:index:allAgree']"
               :disabled="multiple"
            >批量同意</el-button>
         </el-col>
    </el-row>

    <el-table

      :data="orderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" :selectable="selectable"/>
      <!-- <el-table-column type="expand">
        <template #default="props">
          <div m="10" class="ml80">
            <p m="t-0 b-2">开户行: {{ props.row.paymentJson['开户行'] }}</p>
              <p m="t-0 b-2">手机号: {{ props.row.paymentJson['手机号'] }}</p>
              <p m="t-0 b-2">支行: {{ props.row.paymentJson['支行'] }}</p>
              <p m="t-0 b-2">真实姓名: {{ props.row.paymentJson['真实姓名'] }}</p>
              <p m="t-0 b-2">银行卡号: {{ props.row.paymentJson['银行卡号'] }}</p>

          </div>
        </template>
      </el-table-column> -->

      <!-- <el-table-column
        label="提现号"
        width="200"
        prop="applySn"
        align="center"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column
        label="会员Id"
        width="80"
        prop="memId"
        align="center"
        :show-overflow-tooltip="true"
        fixed
      />
      <el-table-column
        label="用户手机账号"
        align="center"
        width="120"
        :show-overflow-tooltip="true"
        fixed
      >
        <template #default="scope">
          <span> {{scope.row.mobile}}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="合伙人"
        width="70"
        prop="isPartner"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="提现金额(元)"
        width="100"
        prop="money"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="手续费(元)"
        width="100"
        prop="chargeMoney"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="实际到账(元)"
        width="100"
        prop="actualMoney"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="提现方式"
        align="center"
        width="100"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span> 银行卡</span>
        </template>
      </el-table-column>

      <el-table-column
        label="真实姓名"
        align="center"
         width="100"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{scope.row.paymentJson['真实姓名']}}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="证件类型"
        align="center"
         width="200"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{scope.row.paymentJson['idTypeName'] ||'-'}}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="证件号码"
        align="center"
         width="200"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{scope.row.paymentJson['idNo']||'-'}}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="开户行"
        align="center"
        width="120"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{scope.row.paymentJson['开户行']}}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="支行"
        align="center"
        width="200"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{scope.row.paymentJson['支行']}}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="银行卡号"
        align="center"
        width="200"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{scope.row.paymentJson['银行卡号']}}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="预留手机号"
        align="center"
        width="120"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span> {{scope.row.paymentJson['手机号']}}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="申请状态"
        width="120"
        prop="statusName"
        align="center"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="拒绝原因"
        align="center"
        prop="createTime"
        :show-overflow-tooltip="true"
        width="150"
      >
        <template #default="scope">
          <span>{{scope.row.auditInfo ||'-'}}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="申请时间"
        align="center"
        prop="createTime"
        width="180"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>


      <el-table-column
        label="操作"
        width="220"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >

        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Pointer"
            @click="handleDeal(scope.row)"
            v-hasPermi="['withdrawal:index:deal']"
            v-if="scope.row.status == 0"
            >处理</el-button
          >
          <el-button
            link
            type="primary"
            icon="View"
            @click="handleView(scope.row)"
            v-hasPermi="['withdrawal:index:view']"
            >查看</el-button
          >
          <el-button
            link
            type="primary"
            icon="EditPen"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['withdrawal:index:check']"
            v-if="scope.row.status == 0||scope.row.status == 1"
            >审核</el-button
          >

          <!-- <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['goods:index:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 审核对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <div class="form_box">
        <el-form ref="orderRef" :model="form" :rules="rules" label-width="80px">

          <el-row>
            <el-col :span="12">
                <el-form-item label="审核结果" prop="auditStatus">
                  <el-select
                      v-model="form.auditStatus"
                      placeholder="请选择审核结果"
                      clearable
                      style="width: 240px"
                    >
                      <el-option
                        v-for="dict in classifyOptions"
                        :key="dict.value"
                        :label="dict.name"
                        :value="dict.value"
                      />
                    </el-select>
                </el-form-item>
              </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="审核信息" prop="auditInfo">
                <el-input
                  v-model="form.auditInfo"
                  placeholder="请输入审核信息"
                  :rows="2"
                  type="textarea"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="打款记录" prop="applyInfo">
                <el-input
                  v-model="form.applyInfo"
                  placeholder="请输入打款记录"
                  :rows="2"
                  type="textarea"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
     <!-- 查看对话框 -->
     <el-dialog :title="detailTitle" v-model="detailOpen" width="800px" append-to-body>
      <div class="form_box">
        <el-descriptions
          class="margin-top"
          :column="2"
          :size="size"
          border
        >
            <el-descriptions-item label="会员id">
              {{ detailObject.memId }}
            </el-descriptions-item>
            <el-descriptions-item label="用户手机账号">
              {{ detailObject.mobile }}
            </el-descriptions-item>
            <el-descriptions-item label="审核状态">
              {{ detailObject.statusName }}
            </el-descriptions-item>
            <el-descriptions-item label="申请时间">
              {{ parseTime(detailObject.createTime) }}
            </el-descriptions-item>
            <el-descriptions-item label="预留手机号">
              {{ detailObject.paymentJson['手机号'] }}
            </el-descriptions-item>
            <el-descriptions-item label="提现金额(元)">
              {{ detailObject.money }}
            </el-descriptions-item>
            <el-descriptions-item label="手续费(元)">
              {{ detailObject.chargeMoney }}
            </el-descriptions-item>
            <el-descriptions-item label="提现方式">
              银行卡
            </el-descriptions-item>
            <el-descriptions-item label="实际到账(元)">
              {{ detailObject.actualMoney }}
            </el-descriptions-item>
            <el-descriptions-item label="真实姓名">
              {{ detailObject.paymentJson['真实姓名'] }}
            </el-descriptions-item>
            <el-descriptions-item label="证件类型">
              {{ detailObject.paymentJson['idTypeName']||'-' }}
            </el-descriptions-item>
            <el-descriptions-item label="证件号码">
              {{ detailObject.paymentJson['idNo'] ||'-'}}
            </el-descriptions-item>
            <el-descriptions-item label="开户行">
              {{ detailObject.paymentJson['开户行'] }}
            </el-descriptions-item>
            <el-descriptions-item label="支行">
              {{ detailObject.paymentJson['支行'] }}
            </el-descriptions-item>
            <el-descriptions-item label="银行卡号">
              {{ detailObject.paymentJson['银行卡号'] }}
            </el-descriptions-item>
            <el-descriptions-item label="提现号">
              {{ detailObject.applySn }}
            </el-descriptions-item>
            <el-descriptions-item label="拒绝原因">
              {{ detailObject.auditInfo ||'-'}}
            </el-descriptions-item>

          </el-descriptions>

      </div>

    </el-dialog>
  </div>
</template>

<script setup name="withdrawal">
import { getCashOutListt, auditCashOutInfo,cashOutExport,dealCashOutInfo,batchDealCashOutInfo,batchAuditPassCashOutInfo } from "@/api/withdrawal/index";
import { getMemberList} from "@/api/member/index";
const { proxy } = getCurrentInstance();

const orderList = ref([]);

const open = ref(false);
const loading = ref(true);
const detailOpen = ref(false);
const total = ref(0);
const title = ref("审核");
const detailTitle = ref("查看");
const multiple = ref(true);
const dateRange = ref([]);
const ids = ref([]);
const memberOptions = ref([]);
const memloading = ref(false);
const classifyOptions = ref([
  {
    name:'同意',
    value:2
  },
  {
    name:'拒绝',
    value:9
  }
])

function selectable(row){
  if(row.status == 0 || row.status == 1){
    return true
  }else{
    return false
  }

}


const stateObeject = ref([
  {
    label:'待审核',
    value:0
  },
  {
    label:'处理中',
    value:1
  },
  {
    label:'已处理',
    value:2
  },
  {
    label:'用户撤回',
    value:8
  },
  {
    label:'已拒绝',
    value:9
  }
])

const data = reactive({
  form: {},
  detailObject: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status:'',
    memId:'',
    payRealName:undefined
  },

  rules: {
    auditStatus: [
      { required: true, message: "处理结果不能为空", trigger: "blur" },
    ]
  },
});

const { queryParams, form, rules, detailObject } = toRefs(data);



/** 批量处理  */
function handAllDeal(row){
  proxy.$modal
    .confirm('是否确认批量处理？')
    .then(function () {
      return batchDealCashOutInfo({ids:ids.value.join(',')});
    })
    .then((res) => {
      getList();
      proxy.$modal.msgSuccess(res.msg);
      ids.value = []

    })
    .catch(() => {});
}

/** 批量 */
function handAllAgree(row){
  proxy.$modal
    .confirm('是否确认批量同意？')
    .then(function () {


      return batchAuditPassCashOutInfo({ids:ids.value.join(',')});
    })
    .then((res) => {
      getList();
      proxy.$modal.msgSuccess(res.msg);
      ids.value = []
    })
    .catch(() => {});
}
/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  multiple.value = !selection.length;
};

/** 查询订单列表 */
function getList() {
  loading.value = true;
  getCashOutListt(proxy.addDateRange(queryParams.value, dateRange.value)).then((response) => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    id: undefined,
    auditStatus: undefined,
    auditInfo: undefined,
    applyInfo: undefined,
  };
  proxy.resetForm("orderRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function handleDeal(row){
  proxy.$modal
    .confirm('是否确认处理？')
    .then(function () {
      return dealCashOutInfo({id:row.id});
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("处理成功");
    })
    .catch(() => {});
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  queryParams.value.memId = '';
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 审核按钮操作 */
function handleUpdate(row) {
  reset();
  detailObject.value = row;
  form.value.id = row.id;
  open.value = true;
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderRef"].validate((valid) => {
    if (valid) {
      auditCashOutInfo(form.value).then((response) => {
        proxy.$modal.msgSuccess("审核成功");
        open.value = false;
        getList();
      });
    }
  });
}

function handleView(row){
  detailObject.value = row
  detailOpen.value = true
}

/** 导出按钮操作 */
function handleExport() {

  cashOutExport(addDateRanges(queryParams.value, dateRange.value)).then((response) => {
    window.location.href = response.data
  });
}

function remoteMethod(query) {
  if (query !== '') {
    memloading.value = true;
    setTimeout(() => {

      getMemberList({mobile:query}).then(response => {
        memberOptions.value = response.data
        memloading.value = false;
      });

    }, 200);
  } else {
    memberOptions.value = [];
  }
}

 function addDateRanges(params, dateRange, propName) {
  let search = params;
  search.params = typeof (search.params) === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};
  dateRange = Array.isArray(dateRange) ? dateRange : [];
  if (typeof (propName) === 'undefined') {
    search.params['beginTime'] = dateRange[0];
    search.params['endTime'] = dateRange[1];
  } else {
    search.params['begin' + propName] = dateRange[0];
    search.params['end' + propName] = dateRange[1];
  }
  delete search.pageNum;
  delete search.pageSize;
  return search;
}
getList();
</script>
<style lang="scss" scoped>
.ml80{
  margin-left: 80px;
}
</style>
